import SwiftUI

// LoginPage
// 登录界面

struct LoginPage: View {
    @Environment(\.modelContext) private var modelContext

    @State private var phoneNumberInput: String = ""
    @State private var passwordInput: String = ""
    
    @State private var showLogo: Bool = true
    @State private var showRegister: Bool = false
    
    @State private var registerStatus: Bool = false
    
    var body: some View {
        ZStack(alignment: .center) {
            // 背景图片
            Image("Background")
                .resizable()
                .ignoresSafeArea()
            
            VStack {
                if showLogo {
                    Spacer()
                    Image("Logo")
                        .resizable()
                        .scaledToFit()
                        .frame(width: 140, height: 140)
                        .padding(.bottom, 20)
                }
                
                Text("登录")
                    .font(.largeTitle)
                    .foregroundColor(.white)
                    .padding(.bottom, 20)
                
                LoginWindow(phoneNumberInput: $phoneNumberInput,
                            passwordInput: $passwordInput,
                            showRegister: $showRegister)
                    .modelContext(modelContext)
                
                Spacer()
            }
        }
        .sheet(isPresented: $showRegister, onDismiss: {
            if(registerStatus) {
                showRegisterAlert()
            }
        }, content: {
            RegisterPage(phoneNumberInput: $phoneNumberInput,
                         passwordInput: $passwordInput,
                         registerStatus: $registerStatus)
        })
        .onReceive(keyboardPublisher) { isKeyboard in
            showLogo = !isKeyboard
        }
    }
}

#Preview {
    LoginPage()
}
